//
//  DownloadViewController.m
//  Everest
//
//  Created by 李其昌 on 14/12/17.
//  Copyright (c) 2014年 ThreeL. All rights reserved.
//

#import "DownloadViewController.h"
#import "DownloadCell.h"
#import "SoundCell.h"
#import "LoadingCell.h"
#import "UIColor+AddColor.h"
#import "NetHandler.h"
#import "Download.h"
#import "AFHTTPRequestOperation.h"
#import "UIProgressView+AFNetworking.h"
#import "FileModel.h"

#define kDocPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]


@interface DownloadViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *albuemTableView;
@property (nonatomic, retain) UITableView *soundTableView;
@property (nonatomic, retain) UITableView *loadingTableView;
@property (nonatomic, retain) UIButton *albumButton;
@property (nonatomic, retain) UIButton *soundButton;
@property (nonatomic, retain) UIButton *downLoadButton;
@property (nonatomic, retain) UIView *albumView;
@property (nonatomic, retain) UIView *soundView;
@property (nonatomic, retain) UIView *downLoadView;
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) UIButton *favourButton;
@property (nonatomic, retain) UIButton *talkButton;
@property (nonatomic, retain) UIButton *deleteButton;
@property (nonatomic, assign, getter=isClicked) BOOL click;
@property (nonatomic, retain) AFURLSessionManager *sessionManager;
@property (nonatomic, retain) AFHTTPRequestOperation *operation;
@property (nonatomic, retain) NSString *documentFolder;
@property (nonatomic, retain) NSString *audioFolder;
@property (nonatomic, retain) NSMutableArray *downingList;
@property (nonatomic, retain) NSMutableArray *finishedList;
- (void)deleteFinishedFile:(FileModel *)selectFile;
- (void)ReloadDownLoadingTable;
@end

@implementation DownloadViewController
- (void)dealloc
{
    [_finishedList release];
    [_downingList release];
    [_audioFolder release];
    [_documentFolder release];
    [_operation release];
    [_sessionManager release];
    [_albumView release];
    [_soundView release];
    [_downLoadView release];
    [_albuemTableView release];
    [_soundTableView release];
    [_loadingTableView release];
    [_albumButton release];
    [_soundButton release];
    [_downLoadButton release];
    [_arr release];
    [_favourButton release];
    [_talkButton release];
    [_deleteButton release];
    [super dealloc];
    
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
//        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
//        [center addObserver:self selector:@selector(receiveNotification:) name:@"value" object:nil];
        [self handleData];
    }
    return self;
}
- (void)handleData
{
    Download *dl = [[Download alloc] init];
    NSString *str = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/download/%@/track/%@?device=iPhone", dl.uid, dl.trackId];
    
    [NetHandler getDataWithUrl:str completion:^(NSData *data) {
        
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //*************************导航栏***********************
    // 隐藏导航控制器
    self.navigationController.navigationBarHidden = YES;
    // 自定义导航栏样式
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(-1, -1, self.view.bounds.size.width+2, 66)];
    // 设置其颜色
    view.backgroundColor = [UIColor colorWithRed:248/255.0 green:248/255.0 blue:248/255.0 alpha:1];
    // 设置layer层的边框
    [[view layer] setBorderWidth:1.1];
    // 设置边框的颜色为
    [[view layer] setBorderColor:[UIColor colorWithRed:216/255.0 green:216/255.0 blue:216/255.0 alpha:1].CGColor];
    // 向主视图中添加
    [self.view addSubview:view];
    // 引用计数
    [view release];
    
    // 专辑按钮
    self.albumButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 15, self.view.bounds.size.height / 667 * 33, self.view.bounds.size.width / 6 * 1, view.bounds.size.height / 5 * 3)];
    // 添加到自定义navigation上
    [view addSubview:self.albumButton];
    // 设置按钮名字
    [self.albumButton setTitle:@"专辑" forState:UIControlStateNormal];
    // 设置按钮字体大小
    self.albumButton.titleLabel.font = [UIFont systemFontOfSize:17];
    // 设置按钮字体颜色
    [self.albumButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.albumButton addTarget:self action:@selector(albumButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
//    self.albumButton.backgroundColor = [UIColor purpleColor];
    [_albumButton release];
    
    // 声音按钮
    self.soundButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 75 * 10 + self.view.bounds.size.width / 6 * 1, self.view.bounds.size.height / 667 * 33, self.view.bounds.size.width / 6 * 1, view.bounds.size.height / 5 * 3)];
    // 添加到自定义navigation上
    [view addSubview:self.soundButton];
    // 设置按钮名字
    [self.soundButton setTitle:@"声音" forState:UIControlStateNormal];
    // 设置按钮字体大小
    self.soundButton.titleLabel.font = [UIFont systemFontOfSize:17];
    // 设置按钮字体颜色
    [self.soundButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.soundButton addTarget:self action:@selector(soundButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
//    self.soundButton.backgroundColor = [UIColor purpleColor];
    [_soundButton release];
    
    // 正在下载按钮
    self.downLoadButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 375 * 210 + self.view.bounds.size.width / 6 * 1, self.view.bounds.size.height / 667 * 33, self.view.bounds.size.width / 4, view.bounds.size.height / 5 * 3)];
    // 添加到自定义navigation上
    [view addSubview:self.downLoadButton];
    // 设置按钮名字
    [self.downLoadButton setTitle:@"正在下载" forState:UIControlStateNormal];
    // 设置按钮字体颜色
    [self.downLoadButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    // 设置按钮字体大小
    self.downLoadButton.titleLabel.font = [UIFont systemFontOfSize:17];
    [self.downLoadButton addTarget:self action:@selector(downLoadButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
//    self.downLoadButton.backgroundColor = [UIColor purpleColor];
    [_downLoadButton release];
    //*********************专辑页面*************************
    // 专辑视图
    self.albumView = [[UIView alloc] initWithFrame:CGRectMake(0, 66, self.view.bounds.size.width, self.view.bounds.size.height - 66)];
    [self.view addSubview:self.albumView];
    [_albumView release];
    // 专辑的tableView
    self.albuemTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -1, self.view.bounds.size.width, self.view.bounds.size.height - 66) style:UITableViewStyleGrouped];
    [self.albumView addSubview:self.albuemTableView];
    self.albuemTableView.dataSource = self;
    self.albuemTableView.delegate = self;
    self.albuemTableView.contentInset = UIEdgeInsetsMake(0, 0, 48, 0);
    [_albuemTableView release];
    
    // 专辑清空按钮
    UIButton *cleanAlbumButton = [UIButton buttonWithType:UIButtonTypeSystem];
    // 添加到专辑视图上
    [self.albumView addSubview:cleanAlbumButton];
    // 按钮位置
    cleanAlbumButton.frame = CGRectMake(0, 65, self.view.bounds.size.width, self.view.bounds.size.height / 12);
    // 按钮颜色
    cleanAlbumButton.backgroundColor = [UIColor lightGrayColor];
    // 按钮名字
    [cleanAlbumButton setTitle:@"一键清空" forState:UIControlStateNormal];
    // 字体颜色
    [cleanAlbumButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    // 字体大小
    cleanAlbumButton.titleLabel.font = [UIFont systemFontOfSize:20];
    [cleanAlbumButton addTarget:self action:@selector(cleanAlbumButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.albuemTableView.tableHeaderView = cleanAlbumButton;
    //*********************声音页面*************************
    // 声音视图
    self.soundView = [[UIView alloc] initWithFrame:CGRectMake(0, 66, self.view.bounds.size.width, self.view.bounds.size.height - 66)];
    [self.view addSubview:self.soundView];
    self.soundView.backgroundColor = [UIColor magentaColor];
    [_soundView release];
    // 声音的tableView
    self.soundTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -1, self.view.bounds.size.width, self.view.bounds.size.height - 66) style:UITableViewStyleGrouped];
    [self.soundView addSubview:self.soundTableView];
    self.soundTableView.dataSource = self;
    self.soundTableView.delegate = self;
    self.soundTableView.contentInset = UIEdgeInsetsMake(0, 0, 48, 0);
    [_soundTableView release];
    
    // 声音清空按钮
    UIButton *cleanSoundButton = [UIButton buttonWithType:UIButtonTypeSystem];
    // 按钮位置
    cleanSoundButton.frame = CGRectMake(0, 65, self.view.bounds.size.width, self.view.bounds.size.height / 12);
    // 按钮颜色
    cleanSoundButton.backgroundColor = [UIColor lightGrayColor];
    // 按钮名字
    [cleanSoundButton setTitle:@"一键清空" forState:UIControlStateNormal];
    // 字体颜色
    [cleanSoundButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    // 字体大小
    cleanSoundButton.titleLabel.font = [UIFont systemFontOfSize:20];
    [cleanSoundButton addTarget:self action:@selector(cleanSoundButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.soundTableView.tableHeaderView = cleanSoundButton;
    //*********************正在下载页面**********************
    // 正在下载视图
    self.downLoadView = [[UIView alloc] initWithFrame:CGRectMake(0, 66, self.view.bounds.size.width, self.view.bounds.size.height - 66)];
    [self.view addSubview:self.downLoadView];
    self.downLoadView.backgroundColor = [UIColor greenColor];
    [_downLoadView release];
    self.loadingTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -1, self.view.bounds.size.width, self.view.bounds.size.height - 66) style:UITableViewStyleGrouped];
    [self.downLoadView addSubview:self.loadingTableView];
    self.loadingTableView.dataSource = self;
    self.loadingTableView.delegate = self;
    self.loadingTableView.contentInset = UIEdgeInsetsMake(0, 0, 48, 0);
    [_loadingTableView release];
    
    // 给tableView的headerView一个view
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 65, self.view.bounds.size.width, self.view.bounds.size.height / 12)];
    self.loadingTableView.tableHeaderView = headerView;
    [headerView release];
    
    // 全部暂停按钮
    UIButton *allPauseButton = [UIButton buttonWithType:UIButtonTypeSystem];
    // 按钮位置
    allPauseButton.frame = CGRectMake(0, 0, self.view.bounds.size.width / 2, self.view.bounds.size.height / 12);
    // 添加到view上
    [headerView addSubview:allPauseButton];
    // 按钮背景颜色
    allPauseButton.backgroundColor = [UIColor lightGrayColor];
    // 按钮名字
    [allPauseButton setTitle:@"全部暂停" forState:UIControlStateNormal];
    // 按钮字体大小
    allPauseButton.titleLabel.font = [UIFont systemFontOfSize:20];
    self.click = NO;
    // 按钮字体颜色
    [allPauseButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    // 给按钮添加方法
    [allPauseButton addTarget:self action:@selector(allPauseButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    // 正在下载一键清空按钮
    UIButton *cleanLoadingButton = [UIButton buttonWithType:UIButtonTypeSystem];
    // 按钮位置
    cleanLoadingButton.frame = CGRectMake(self.view.bounds.size.width / 2, 0, self.view.bounds.size.width / 2, self.view.bounds.size.height / 12);
    // 添加到view上
    [headerView addSubview:cleanLoadingButton];
    // 按钮颜色
    cleanLoadingButton.backgroundColor = [UIColor lightGrayColor];
    // 按钮名字
    [cleanLoadingButton setTitle:@"一键清空" forState:UIControlStateNormal];
    // 字体颜色
    [cleanLoadingButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    // 字体大小
    cleanLoadingButton.titleLabel.font = [UIFont systemFontOfSize:20];
    // 给按钮添加方法
    [cleanLoadingButton addTarget:self action:@selector(cleanLoadingButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
}

// section的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // 专辑的
    if (tableView == self.albuemTableView) {
        return 5;
        // 声音的
    } else if (tableView == self.soundTableView) {
        return 10;
        // 正在下载的
    } else {
        return 1;
    }
}
// section的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    } else {
        return 10;
    }
}
// cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
// cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 专辑的tableView
    if (tableView == self.albuemTableView) {
        NSString *title = @"标题栏标题栏标题栏标题栏标题栏标题栏标题栏标题栏标题栏标题栏";
        NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
        
        CGRect rect = [title boundingRectWithSize:CGSizeMake(self.albuemTableView.bounds.size.width / 2, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        return rect.size.height + self.view.bounds.size.height / 667 * 35;
    } else if (tableView == self.soundTableView) {
        
        // 声音的tableView
        NSString *title = @"声音声音声音声音声音声音声音声音声音声音声音声音声音声音声音";
        NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
        CGRect rect = [title boundingRectWithSize:CGSizeMake(self.soundTableView.bounds.size.width / 2, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        return rect.size.height + self.view.bounds.size.height / 667 * 55;
    } else {
        
        //正在下载的tableView
        NSString *title = @"正在下载正在下载正在下载正在下载正在下载正在下载正在下载正在下载正在下载正在下载";
        NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
        CGRect rect = [title boundingRectWithSize:CGSizeMake(self.loadingTableView.bounds.size.width / 2, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        return rect.size.height + self.view.bounds.size.height / 667 * 55;
    }
}
// 向cell中添加内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 专辑的
    if (tableView == self.albuemTableView) {
        static NSString *str = @"reuse";
        DownloadCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
        if (cell == nil) {
            cell = [[DownloadCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
        }
//        cell.titleLabel.text = @"标题栏标题栏标题栏标题栏标题栏标题栏标题栏标题栏标题栏标题栏";
        return cell;
        // 声音的
    } else if (tableView == self.soundTableView){
        static NSString *str = @"reuseSound";
        SoundCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
        if (cell == nil) {
            cell = [[SoundCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
        }
//        cell.titleLabel.text = @"声音声音声音声音声音声音声音声音声音声音声音声音声音声音声音";
        return cell;
        // 正在下载的
    } else {
        static NSString *str = @"reuseLoading";
        LoadingCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
        if (cell == nil) {
            cell = [[LoadingCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
        }
        cell.titleLabel.text = @"正在下载正在下载正在下载正在下载正在下载正在下载正在下载正在下载正在下载正在下载";
        return cell;
    }
}
// 专辑按钮方法
- (void)albumButtonClicked:(UIButton *)button
{
    NSLog(@"专辑按钮");
    [self.albumButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [self.soundButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.downLoadButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view bringSubviewToFront:self.albumView];
}
// 声音按钮方法
- (void)soundButtonClicked:(UIButton *)button
{
    NSLog(@"声音按钮");
    [self.soundButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [self.albumButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.downLoadButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view bringSubviewToFront:self.soundView];
    [self.soundTableView reloadData];
}
// 正在下载按钮方法
- (void)downLoadButtonClicked:(UIButton *)button
{
    NSLog(@"正在下载按钮");
    [self.downLoadButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [self.soundButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.albumButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view bringSubviewToFront:self.downLoadView];
    [self.loadingTableView reloadData];
}
// 专辑一键清空按钮方法
- (void)cleanAlbumButtonClicked:(UIButton *)button
{
    NSLog(@"专辑一键清空");
}
// 声音一键清空按钮方法
- (void)cleanSoundButtonClicked:(UIButton *)button
{
    NSLog(@"声音一键清空");
}
// 正在下载全部暂停按钮方法
- (void)allPauseButtonClicked:(UIButton *)button
{
    if (self.click == NO) {
        NSLog(@"全部暂停");
        [button setTitle:@"全部开始" forState:UIControlStateNormal];
        
    } else {
        [button setTitle:@"全部暂停" forState:UIControlStateNormal];
    }
    self.click = !self.click;
}
// 正在下载一键清空按钮方法
- (void)cleanLoadingButtonClicked:(UIButton *)button
{
    NSLog(@"正在下载一键清空");
}
// 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (tableView == self.albuemTableView) {
        NSLog(@"点击专辑cell");
    } else if (tableView == self.soundTableView) {
        NSLog(@"点击声音cell");
    } else {
        // 下载
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    NSLog(@"%@", kDocPath);
}

//下载成功
- (void)downloadWithUrl:(NSString *)downloadUrl tag:(NSInteger)tag
{
    
    NSString *url = downloadUrl;
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    //不对数据解析
    
    //无参数的GET请求下载parameters:nil,也可以带参数
    AFHTTPRequestOperation *operation = [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        //下载完成后打印这个
        NSLog(@"下载完成");
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"下载失败");
        
    }];
    
    //定义下载路径
    NSString *filePath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/%@",[url lastPathComponent]];
    
    NSLog(@"%@",filePath);
    
    //创建下载文件
    if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        
        //会自动将数据写入文件
        [[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
        
    }
    
    //使用AF封装好的类目UIProgressView+AFNetworking.h
    if (operation != nil) {
        //下载进度
        UIProgressView *progressView = (UIProgressView *)[self.view viewWithTag:tag];
        [progressView setProgressWithDownloadProgressOfOperation:operation animated:YES];
    }
    
    //设置下载的输出流，而此输出流写入文件,这里可以计算传输文件的大小
    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:YES];
    
    //监听的下载的进度
    /*
     *
     * bytesRead 每次传输的数据包大小
     * totalBytesRead 已下载的数据大小
     * totalBytesExpectedToRead 总大小
     *
     */
    
    //调用Block监听下载传输情况
    
    [operation setDownloadProgressBlock:^(NSUInteger bytesRead, NSInteger totalBytesRead, NSInteger totalBytesExpectedToRead) {
        CGFloat progess = totalBytesRead / (CGFloat)totalBytesExpectedToRead;
        
        UIProgressView *progressView = (UIProgressView *)[self.view viewWithTag:tag];
        
        progressView.progress = progess;
    }];
    
}


- (void)receiveNotification:(NSNotification *)noti
{
    NSLog(@"%@",noti.object);
    NSString *str = [NSString stringWithFormat:@"%@",noti.object];
    self.documentFolder = [kDocPath stringByAppendingFormat:@"/%@", str];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    self.audioFolder = [self.documentFolder stringByAppendingFormat:@"/audio"];
    NSError *error = nil;
    BOOL result = [fileManager createDirectoryAtPath:self.audioFolder withIntermediateDirectories:YES attributes:nil error:&error];
    if (result) {
        NSLog(@"文件夹创建成功");
    } else {
        NSLog(@"文件夹创建失败");
    }
}




// 尾部高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (tableView == self.albuemTableView) {
        return 0;
    } else if (tableView == self.soundTableView) {
        return self.view.bounds.size.height / 19;
    } else {
        return self.view.bounds.size.height / 40;
    }
}
// 尾部添加的东西
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (tableView == self.soundTableView) {
        UIView *threeButtonView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height / 19)] autorelease];
        
        // "评论"
        self.favourButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, threeButtonView.frame.size.width / 3, threeButtonView.frame.size.height)];
        [threeButtonView addSubview:self.favourButton];
        [self.favourButton setTitle:@"赞" forState:UIControlStateNormal];
        self.favourButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [self.favourButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        self.favourButton.backgroundColor = [UIColor lightGrayColor];
        
        // "评论"
        self.talkButton = [[UIButton alloc] initWithFrame:CGRectMake(threeButtonView.frame.size.width / 3, 0, threeButtonView.frame.size.width / 3, threeButtonView.frame.size.height)];
        [threeButtonView addSubview:self.talkButton];
        [self.talkButton setTitle:@"评论" forState:UIControlStateNormal];
        self.talkButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [self.talkButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        self.talkButton.backgroundColor = [UIColor lightGrayColor];
        
        // "下载"
        self.downLoadButton = [[UIButton alloc] initWithFrame:CGRectMake(threeButtonView.frame.size.width / 3 * 2, 0, threeButtonView.frame.size.width / 3, threeButtonView.frame.size.height)];
        [threeButtonView addSubview:self.downLoadButton];
        [self.downLoadButton setTitle:@"删除" forState:UIControlStateNormal];
        self.downLoadButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [self.downLoadButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        self.downLoadButton.backgroundColor = [UIColor lightGrayColor];
        
        return threeButtonView;
        
    } else if (tableView == self.loadingTableView) {
        UIView *loadLine = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height / 40)] autorelease];
        
        UIProgressView *progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
        progressView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height / 40 / 2);
        progressView.progress = 0.0;
        progressView.tag = section;
        [loadLine addSubview:progressView];
        return loadLine;
    }
    return nil;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
